# Compiler and flags
CXX := g++
CXXFLAGS := -std=c++17 -Wall -Wextra -g -Iinclude -Ilib/cppjieba -Ilib -Ilib
LDFLAGS := -Llib -pthread -llog4cpp

# Directories
SRC_DIR := src
OBJ_DIR := obj
BIN_DIR := bin
INC_DIR := include

# Source files
SRC_SUBDIRS := $(shell find $(SRC_DIR) -type d)
SOURCES := $(foreach dir,$(SRC_SUBDIRS),$(wildcard $(dir)/*.cc))
OBJECTS := $(patsubst $(SRC_DIR)/%.cc,$(OBJ_DIR)/%.o,$(SOURCES))
DEPENDS := $(OBJECTS:.o=.d)

# Targets
TARGET := $(BIN_DIR)/test

# Default target
all: $(TARGET)

# Main executable
$(TARGET): $(OBJECTS)
	@mkdir -p $(@D)
	$(CXX) $^ -o $@ $(LDFLAGS)

# Compile with dependency generation
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cc
	@mkdir -p $(@D)
	$(CXX) $(CXXFLAGS) -MMD -MP -c $< -o $@

# Include auto-generated dependencies
-include $(DEPENDS)

# Clean
clean:
	rm -rf $(OBJ_DIR)/*
	rm -f $(TARGET)

.PHONY: all clean
